//
//  // BDLoginPasswordViewController.m
//  BadBoy
//
//  Created by wen on 2017/3/30.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDLoginPasswordViewController.h"
#import "BDForgetPasswordViewController.h"
#import "BDUserModel.h"
#import "BDMD5.h"
#import "BDDetailLoginViewController.h"

@interface  BDLoginPasswordViewController ()
{
    BOOL waitCityList;
}
@property (nonatomic, strong) UITextField *userField;
@property (nonatomic, strong) UITextField *passField;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *findButton;
@end

@implementation  BDLoginPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"密码登录");
    self.userField.keyboardType = UIKeyboardTypeNumberPad;
    self.passField.keyboardType = UIKeyboardTypeDefault;
    [self bindRAC];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)bindRAC{
    [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSString *alert = nil;
        if (self.userField.text.length != 11) {
            alert = ASLocalizedString(@"请填写合法的手机号");
        }
        else if (self.passField.text.length == 0) {
            alert = ASLocalizedString(@"请填写密码");
        }else if (!(self.passField.text.length >= 6 && self.passField.text.length <= 16)) {
            alert = ASLocalizedString(@"密码长度不符，请重新填写");
        }
        if (alert != nil) {
            [MBManager showCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]] title:alert clickHidden:NO clickBlock:nil];
            return;
        }
        [MBManager showLoadingAlert:ASLocalizedString(@"正在登陆")clickHidden:NO clickBlock:nil];
        [BDUserModel loginUserWithAccount:self.userField.text code:self.passField.text loginType:BDLoginTypePassword completionHandler:^(id obj, NSError *error) {
            if (error) {
                if (error.code == 40104) {
                    [MBManager hideAlert];
                    [self.view alertWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"该账号没有注册，可以通过验证码快捷登录并注册")type:AlertTypeAlert cancleTitle:ASLocalizedString(@"确定")otherTitle:nil clickBlock:^(NSInteger index) {
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    NSString *errorString = @"用户名或密码错误";
                    if (error.code == BDLoginErrorLoginIMFailed) {
                        errorString = @"极光IM登录失败";
                    }else if (error.code == BDLoginErrorGetFirendsListFailed) {
                        errorString = @"获取好友列表失败";
                    }else if (error.code == BDLoginErrorGetGroupsListFailed) {
                        errorString = @"获取好友列表失败";
                    }
                    [MBManager showCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]] title:ASLocalizedString(errorString)clickHidden:NO clickBlock:nil];
                }
            }else{
                [MBManager hideAlert];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateUnreadCount" object:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"getConvitasionFromDB" object:nil];
                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            }
        }];
    }];
    
    //变更登录方式按钮
    [[self.findButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        // BDLog(ASLocalizedString(@"忘记密码"));
        BDForgetPasswordViewController *forgetVC = [[BDForgetPasswordViewController alloc] init];
        forgetVC.type = ASLocalizedString(@"修改密码");
        [self.navigationController pushViewController:forgetVC animated:YES];
    }];
}

- (void)pushDetailWithType:(BDRegistType)registType{
    BDDetailLoginViewController *detailLoginVC = [[BDDetailLoginViewController alloc] init];
    detailLoginVC.editType = BDEditTypePhone;
    detailLoginVC.registType = registType;
    detailLoginVC.phone = self.userField.text;
    detailLoginVC.code = self.passField.text;
    [self.navigationController pushViewController:detailLoginVC animated:YES];
}

- (UITextField *)userField{
    if (!_userField) {
        UIView *bkView = [[UIView alloc] initWithFrame:CGRectMake(0, kBDTopHeight + 10, self.view.bounds.size.width, 45)];
        bkView.backgroundColor = [UIColor whiteColor];
        
        UIView *upLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0.5)];
        upLine.backgroundColor = kBDColorGray;
        [bkView addSubview:upLine];
        
        [self.view addSubview:bkView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 40)];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
        titleLabel.text = ASLocalizedString(@"手机号码");
        titleLabel.centerY = bkView.height / 2.0;
        [bkView addSubview:titleLabel];
        
        self.userField = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, self.view.width - 40, 45)];
        _userField.font = [UIFont systemFontOfSize:14];
        _userField.tintColor = [UIColor lightGrayColor];
        _userField.borderStyle = UITextBorderStyleNone;
        _userField.keyboardType = UIKeyboardTypeNumberPad;
        _userField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [bkView addSubview:_userField];
    }
    return _userField;
}

- (UITextField *)passField{
    if (!_passField) {
        UIView *bkView = [[UIView alloc] initWithFrame:CGRectMake(0, kBDTopHeight + 10 + 45, self.view.bounds.size.width, 45)];
        bkView.backgroundColor = [UIColor whiteColor];
        bkView.layer.borderWidth = 0.5;
        bkView.layer.borderColor = kBDColorGray.CGColor;
        [self.view addSubview:bkView];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 40)];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
        titleLabel.text = ASLocalizedString(@"密码");
        titleLabel.centerY = bkView.height / 2.0;
        [bkView addSubview:titleLabel];
        
        self.passField = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, self.view.bounds.size.width - 40, 45)];
        _passField.font = [UIFont systemFontOfSize:14];
        _passField.tintColor = [UIColor lightGrayColor];
//        _passField.borderStyle = UITextBorderStyleNone;
        _passField.secureTextEntry = YES;
        [bkView addSubview:_passField];
    }
    return _passField;
}

- (UIButton *)loginButton{
    if (!_loginButton) {
        self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.frame = CGRectMake(10, kBDTopHeight + 10 + 90 + 20, self.view.bounds.size.width - 20, 45);
        [_loginButton setTitle:ASLocalizedString(@"登录")forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginButton.backgroundColor = kLoginButtonColor;
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _loginButton.layer.masksToBounds = YES;
        _loginButton.layer.cornerRadius = 4;
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}

- (UIButton *)findButton{
    if (!_findButton) {
        self.findButton = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *title = ASLocalizedString(@"忘记密码 >>");
        [_findButton setTitle:title forState:UIControlStateNormal];
        [_findButton setTitleColor:KBDColorBlue forState:UIControlStateNormal];
        _findButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _findButton.size = CGSizeMake([title widthWithFont:_findButton.titleLabel.font], _findButton.titleLabel.font.pointSize);
        _findButton.top = self.loginButton.bottom + 20;
        _findButton.right = kBDScreenWidth - 10;
        [self.view addSubview:_findButton];
    }
    return _findButton;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
